import React, { Component } from 'react';
import { StyleSheet, Text, View, ActivityIndicator } from 'react-native';
export default class Loading extends Component {
    constructor(props) {
        super(props);
        this.state = {
            show:false,
            alreadyHide:false,
            loadingText:this.props.loadingText,
        };
        if(props.onRef){
            props.onRef(this)
          } 
    }

    show = (loadingText) => {
        if (loadingText) {
            this.setState({
                loadingText: loadingText
            })
        }
        this.setState({show: true})
    };

    showAfterDely = (afterDely) => {
        if (afterDely) {
            setTimeout(() => {
                if (!this.state.alreadyHide) {
                    this.setState({show: true})
                }
            }, afterDely*1000);
        } else {
            this.setState({show: true})
        }
    };

    hide = () => {
        this.setState({show: false, alreadyHide:true})
    };

    render() {
        if (this.state.show) {
            return (
                <View style={styles.LoadingPage}>
                    <View style={{
                        paddingHorizontal: 12,
                        minWidth: 100,
                        height: 100,
                        backgroundColor: "rgba(0,0,0,0.6)",
                        opacity: 1,
                        justifyContent: "center",
                        alignItems: "center",
                        borderRadius:7
                    }}>
                        <ActivityIndicator size="large" color="#FFF" />
                        <Text style={{ marginLeft: 10,color:"#FFF",marginTop:10 }}>{this.state.loadingText && this.state.loadingText != '' ?this.state.loadingText :'正在加载...'}</Text>
                    </View>
                </View>
            );
        } else {
            return <View />
        }
    }
} 
const styles = StyleSheet.create({
    LoadingPage: {
        position: "absolute",
        left: 0,
        top: 0,
        backgroundColor: "rgba(0,0,0,0)",
        width: Constant.screen_width,
        height: Constant.screen_height,
        justifyContent: "center",
        alignItems: "center",
    },
});

